package com.lancoo.znbkxx.teacher.base;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.lancoo.znbkxx.teacher.Ext;
import com.lancoo.znbkxx.teacher.aibkTestCoach.auidoPlayer.AudioTracker;
import com.lancoo.znbkxx.teacher.widget.voicePlayer.VoicePlayer;

import java.io.IOException;

public abstract class BasePysjFragment extends Fragment {

    private static final String STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN";

    private View mView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(createLayout(), container, false);
            initView(mView);
        } else {
            ViewGroup viewGroup = (ViewGroup) mView.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(mView);
            }
        }
        return mView;
    }

    protected abstract int createLayout();

    protected abstract void initView(View view);

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 如果savedInstanceState不为空，那么可以获取在onSaveInstanceState方法中保存的值。
        if (savedInstanceState != null) {
            boolean isHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);// 获取保存fragment前的可视状态

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            if (isHidden) {
                // 如果原来fragment是隐藏状态，那么就hide
                transaction.hide(this);
            } else {
                // 如果原来fragment是显示状态，那么就show
                transaction.show(this);
            }

            transaction.commit();
        }

    }


    /**
     * 该方法在onCreate之前调用，实现数据的预加载
     *
     * @param isVisibleToUser 界面对用户是否可见
     *                        false表示对用户不可见，反之为true
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            onVisible();
        } else {
            onInVisible();
        }
    }

    /**
     * 界面对用户不可见时，执行该方法
     */
    protected void onInVisible() {

    }

    /**
     * 界面对用户可见时，执行加载数据的方法
     */
    protected void onVisible() {
        setLazyLoad();
    }

    /**
     * 加载数据方法
     */
    protected void setLazyLoad() {

    }

    /**
     * 调用该方法用来保存fragment当前的动态状态。
     * 可以用bundle对象保存一些值，然后可以在onCreate方法中获取保存的值。
     *
     * @param outState Bundle对象
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());// 保存当前fragment的可视状态
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mVoicePlayer != null) {
            mVoicePlayer.stop();
        }
        mVoicePlayer = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mVoicePlayer != null) {
            mVoicePlayer.stop();
        }
        mVoicePlayer = null;
    }

    /************************音频播放器************************************/
    protected VoicePlayer mVoicePlayer;

    public void rePlay() {
        if (mVoicePlayer != null) {
            mVoicePlayer.rePlay();
        }
    }

    public void play() {
        if (mVoicePlayer != null) {
            mVoicePlayer.play();
        }
    }

    public void pause() {
        if (mVoicePlayer != null) {
            mVoicePlayer.pause();
        }
    }

    public void stop() {
        if (mVoicePlayer != null) {
            mVoicePlayer.stop();
        }
        mVoicePlayer = null;
    }
    /*************************************************************/
    /****************************作文音频播放*******************************************/
    /*******************************************************************************/
    private MediaPlayer mediaPlayer;

    public void initZwAudioPlayer(String audioPath, OnAudioClickListener onAudioClickListener) {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置媒体流类型
        mediaPlayer.setOnPreparedListener(mediaPlayer -> onAudioClickListener.startPlayAudio());
        mediaPlayer.setOnCompletionListener(mediaPlayer -> onAudioClickListener.finishAudio());

        new Thread(() -> {
            try {
                mediaPlayer.setDataSource(Ext.OBSINFO.getServerWithBucket() + audioPath);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void playZwPysjAudio() {
        if (mediaPlayer != null) {
            mediaPlayer.start();
        }
    }

    public void pauseZwPysjAudio() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            mediaPlayer.seekTo(0);
        }
    }

    public interface OnAudioClickListener {
        void startPlayAudio();

        void finishAudio();
    }

    protected AudioTracker audioTracker;

    public void initAudioTracker(String audioPath) {
        String downLoadUrl;
        downLoadUrl = Ext.OBSINFO.getServerWithBucket() + audioPath;
        int i = downLoadUrl.lastIndexOf("/");
        final String audioName = downLoadUrl.substring(i + 1);
//        audioTracker.downLoadAudio(downLoadUrl, audioName);
    }

    public void audioTrackerStop() {
        if (audioTracker != null) {
            audioTracker.stop();
        }
    }

}
